Skip to content

fix: resolve circular dependencies causing KeyEncoding undefined#928

Merged
boorad merged 2 commits into
mainfrom
fix/circular-dependencies
Feb 15, 2026
Merged

fix: resolve circular dependencies causing KeyEncoding undefined#928
boorad merged 2 commits into
mainfrom
fix/circular-dependencies

Conversation

@boorad
Copy link
Copy Markdown
Collaborator

@boorad boorad commented Feb 15, 2026

Summary

Resolves a circular dependency chain that caused KeyEncoding to be undefined at runtime, breaking key generation operations (RSA, EC, Ed, DSA, DH).

Fixes #927

Changes

Break the import cycle

  • conversion.ts: Removed direct import { KeyObject } from keys/classes. Instead, duck-types KeyObject via Symbol.toStringTag ([object KeyObject]) to avoid the circular dependency while preserving correct behavior.
  • dhKeyPair.ts, dsa.ts, ec.ts, ed.ts, rsa.ts: Changed imports from ./keys (barrel export) to ./keys/classes (direct), breaking the cycle path through the barrel's re-exports of ../utils.

CI guardrail

  • Added dpdm circular dependency checker as a devDependency and "circular" script in the package.
  • Added a "Check circular dependencies" step to the validate-js.yml workflow to prevent regressions.

Testing

  • bun tsc passes cleanly
  • bun circular reports no circular dependencies
  • Key generation operations (RSA, EC, Ed, DSA, DH) that previously failed with KeyEncoding undefined should now work correctly

Break circular import cycles that could cause `TypeError: Cannot read
property 'PKCS1' of undefined` depending on bundler module evaluation
order.

- Replace `instanceof KeyObject` in utils/conversion.ts with
  Symbol.toStringTag check to eliminate utils -> keys/classes cycle
- Change 5 algorithm files (ed, rsa, ec, dsa, dhKeyPair) to import
  from keys/classes directly instead of the keys/ barrel, breaking
  keys/index -> generateKeyPair -> algorithm -> keys/index cycles
- Add dpdm circular dependency detection to CI (validate-js workflow)
@boorad boorad self-assigned this Feb 15, 2026
@boorad boorad merged commit 039e1e0 into main Feb 15, 2026
3 checks passed
@boorad boorad deleted the fix/circular-dependencies branch February 15, 2026 15:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

🐛 [TypeError: Cannot read property 'PKCS1' of undefined]

1 participant